'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireWildcard(require('react'));
var _hooks = require('@szzj/hooks');
var _antd = require('antd');
var _createField = _interopRequireDefault(require('../../../createField'));
var _useOptions2 = _interopRequireDefault(require('../../../hooks/useOptions'));
var _FieldContext = require('../../../contexts/FieldContext');
var _excluded = ['options', 'searchable', 'useFirstOption'];
var Select = function Select(_ref) {
  var optionsProp = _ref.options,
    searchable = _ref.searchable,
    useFirstOption = _ref.useFirstOption,
    rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var _useOptions = (0, _useOptions2.default)(optionsProp),
    options = _useOptions.options,
    fetchOptions = _useOptions.fetchOptions;
  var _useFieldContext = (0, _FieldContext.useFieldContext)(),
    label = _useFieldContext.label;
  var innerProps = {};
  var _search = (0, _react.useCallback)(
    function (val) {
      if (searchable && typeof optionsProp === 'function') fetchOptions(val);
    },
    [searchable],
  );
  var _useDebounce = (0, _hooks.useDebounce)(_search),
    search = _useDebounce.execute,
    cancel = _useDebounce.cancel;
  if (searchable && typeof optionsProp === 'function') {
    innerProps.showSearch = true;
    innerProps.onClear = function () {
      cancel();
      fetchOptions();
    };
    innerProps.onSearch = search;
    innerProps.filterOption = function () {
      return true;
    };
  }
  (0, _react.useEffect)(
    function () {
      var existed =
        (rest === null || rest === void 0 ? void 0 : rest.value) && options.length
          ? options.some(function (option) {
              var _rest$value;
              return rest.mode && ['multiple', 'tags'].includes(rest.mode)
                ? (_rest$value = rest.value) === null || _rest$value === void 0
                  ? void 0
                  : _rest$value.includes(option.value)
                : rest.value === option.value;
            })
          : false;
      if (!existed) {
        // 使用首项
        if (useFirstOption && (options === null || options === void 0 ? void 0 : options.length)) {
          if (rest.mode && ['multiple', 'tags'].includes(rest.mode)) {
            var _rest$onChange;
            (_rest$onChange = rest.onChange) === null || _rest$onChange === void 0
              ? void 0
              : _rest$onChange.call(rest, rest.labelInValue ? [options[0]] : [options[0].value], [
                  options[0],
                ]);
          } else {
            var _rest$onChange2;
            (_rest$onChange2 = rest.onChange) === null || _rest$onChange2 === void 0
              ? void 0
              : _rest$onChange2.call(
                  rest,
                  rest.labelInValue ? options[0] : options[0].value,
                  options[0],
                );
          }
          return;
        }
        // 重置为 undefined
        if (rest === null || rest === void 0 ? void 0 : rest.value) {
          if (rest.mode && ['multiple', 'tags'].includes(rest.mode)) {
            var _rest$onChange3;
            (_rest$onChange3 = rest.onChange) === null || _rest$onChange3 === void 0
              ? void 0
              : _rest$onChange3.call(rest, [], []);
          } else {
            var _rest$onChange4;
            // @ts-ignore
            (_rest$onChange4 = rest.onChange) === null || _rest$onChange4 === void 0
              ? void 0
              : _rest$onChange4.call(rest, undefined, undefined);
          }
        }
      }
    },
    [options],
  );
  return /*#__PURE__*/ _react.default.createElement(
    _antd.Select,
    (0, _objectSpread2.default)(
      (0, _objectSpread2.default)(
        (0, _objectSpread2.default)(
          {
            placeholder: '\u8BF7\u9009\u62E9'.concat(label),
          },
          innerProps,
        ),
        rest,
      ),
      {},
      {
        options: options,
      },
    ),
  );
};
var _default = (0, _createField.default)(Select);
exports.default = _default;
